<?php
date_default_timezone_set("Asia/Shanghai");
error_reporting(E_ERROR | E_WARNING | E_PARSE | E_NOTICE);
 
$conn_args = array(
    'host' => 'localhost',
    'port' => 5672,
    'login' => 'admin',
    'password' => 'admin2021',
    'vhost' => 'why'
);
 
$ex_name = 'test_exchange';
$queue_name = 'queue1';
$route = 'why.*';
 
$conn = new AMQPConnection($conn_args);
if(!$conn->connect())
{
    die("connect error");
}
 
$channel = new AMQPChannel($conn);
 
$exchange = new AMQPExchange($channel);
$exchange->setName($ex_name);
$exchange->setType('topic');
$exchange->declare();
 
 
$queue = new AMQPQueue($channel);
$queue->setName($queue_name);
$queue->setFlags(AMQP_DURABLE);
$queue->declare();
$queue->bind($ex_name, $route);  
 
while(true)
{
    $queue->consume('consume_msg');
}
 
$conn->disconnect();
function consume_msg($envelope, $queue){
    $msg = $envelope->getBody();
    //echo $msg."\n";
	file_put_contents("./consumer.log", var_export($msg."\n", true), FILE_APPEND);
    $queue->ack($envelope->getDeliveryTag());
}